using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;
using System.Security.Cryptography;
using System.Threading;

namespace FlexminHost
{
	public partial class PasswordInputForm : Form
	{
		private byte[] Hash;

		private bool EnableFn = false;

		public PasswordInputForm(byte[] hash)
		{
			Hash = hash;
			InitializeComponent();
		}

		public bool AllowFunction = false;

		private void PasswordInputForm_FormClosing(object sender, FormClosingEventArgs e)
		{
			if (EnableFn)
				AllowFunction = true;
			else
				AllowFunction = false;
		}

		private void cancelBtn_Click(object sender, EventArgs e)
		{
			AllowFunction = false;
			Close();
		}

		private void okBtn_Click(object sender, EventArgs e)
		{

			SHA512 sha512 = new SHA512Managed();

			byte[] iHash = sha512.ComputeHash(Encoding.UTF8.GetBytes(PasswordInputBox.Text));

			if (iHash.Length != Hash.Length)
			{
				MessageBox.Show("Wrong password!", "Password error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
				AllowFunction = false;
				return;
			}

			for (int iByte = 0; iByte < iHash.Length; iByte++)
			{
				if (iHash[iByte] != Hash[iByte])
				{
					MessageBox.Show("Wrong password!", "Password error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
					AllowFunction = false;
					return;
				}
			}
			EnableFn = true;
			
			Close();
		}
	}
}